﻿using Renci.SshNet;
using RuYiAdmin.Net.Common.Global;
using System;

namespace RuYiAdmin.Net.Common.Utility
{
    /// <summary>
    /// SshNet工具类
    /// </summary>
    public class RuYiSshNetContext
    {
        /// <summary>
        /// SftpClient Lazy对象
        /// </summary>
        private static readonly Lazy<SftpClient> LazySftpInstance = new Lazy<SftpClient>(() =>
        {
            var sftpClient = new SftpClient(RuYiAdminGlobalContext.SshNetConfig.SftpConfig.Host,
                                            RuYiAdminGlobalContext.SshNetConfig.SftpConfig.UserName,
                                            RuYiAdminGlobalContext.SshNetConfig.SftpConfig.Password
                    );
            return sftpClient;
        });

        /// <summary>
        /// SftpClient单例对象
        /// </summary>
        public static SftpClient SftpInstance { get { return LazySftpInstance.Value; } }

        /// <summary>
        /// SftpClient是否已创建
        /// </summary>
        public static bool IsSftpInstanceCreated { get { return LazySftpInstance.IsValueCreated; } }

        /// <summary>
        /// SshClient Lazy对象
        /// </summary>
        private static readonly Lazy<SshClient> LazySshInstance = new Lazy<SshClient>(() =>
        {
            var sshClient = new SshClient(RuYiAdminGlobalContext.SshNetConfig.SshConfig.Host,
                                          RuYiAdminGlobalContext.SshNetConfig.SshConfig.UserName,
                                          RuYiAdminGlobalContext.SshNetConfig.SshConfig.Password
                    );
            return sshClient;
        });

        /// <summary>
        /// SshClient单例对象
        /// </summary>
        public static SshClient SshInstance { get { return LazySshInstance.Value; } }

        /// <summary>
        /// SshClient是否已创建
        /// </summary>
        public static bool IsSshInstanceCreated { get { return LazySshInstance.IsValueCreated; } }

        /// <summary>
        /// ScpClient Lazy对象
        /// </summary>
        private static readonly Lazy<ScpClient> LazyScpInstance = new Lazy<ScpClient>(() =>
        {
            var scpClient = new ScpClient(RuYiAdminGlobalContext.SshNetConfig.ScpConfig.Host,
                                          RuYiAdminGlobalContext.SshNetConfig.ScpConfig.UserName,
                                          RuYiAdminGlobalContext.SshNetConfig.ScpConfig.Password
                    );
            return scpClient;
        });

        /// <summary>
        /// ScpClient 单例对象
        /// </summary>
        public static ScpClient ScpInstance { get { return LazyScpInstance.Value; } }

        /// <summary>
        /// ScpClient 是否已创建
        /// </summary>
        public static bool IsScpInstanceCreated { get { return LazyScpInstance.IsValueCreated; } }
    }
}
